package demo150;

import java.util.HashMap;
import java.util.Map;

public class romanToInt {

    public int romanToInt(String s) {
        Map<Character,Integer> index = new HashMap<Character, Integer>(){{
            put('I',1);
            put('V',5);
            put('X',10);
            put('L',50);
            put('C',100);
            put('D',500);
            put('M',1000);
        }};
        int ret = 0;
        int n = s.length();
        for(int i =0;i < n;i++) {
            if(i<n-1 &&index.get(s.charAt(i)) < index.get(s.charAt(i+1))) {
                ret -= index.get(s.charAt(i));
            }else {
                ret  += index.get(s.charAt(i));
            }
        }
        return ret;

    }
}
